package light.head.controller.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.ReturnDetailDAO;
import light.head.dao.oa.ReturnInfoDAO;
import light.head.dao.oa.ReturnItemDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.ReturnDetail;
import light.head.model.ReturnInfo;
import light.head.model.ReturnItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/***
 * 我的物品归还信息
 * @author yy
 *
 */
@Controller
@RequestMapping(value = Url.C_MY_RETURN)
public class MyReturnController extends SupporterController{
	
	
	private ReturnInfoDAO retDAO;
	private ReturnItemDAO itemDAO;
	private ReturnDetailDAO detailDAO;
	 
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView(Url.MY_RETURN_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception{
		PageBean pb = createPageBean(request);
		Map<String, String>map = createQueryMapByParmName(request, Parm.CN,Parm.CT,Parm.AFFIRM);
		map.put(Parm.RU, cu_code(request).toString());
			if (RESULT.SUCCESS == retDAO.findByCu(cu_code(request), pb, map)) {
				List<Object> conCodes = new ArrayList<Object>();
				for (Object o : pb.getResultList()) {
					ReturnInfo ret = (ReturnInfo) o;
					conCodes.add(ret.getCode());
				}
				// 将对应的明细放到对应的领用信息里
				List<ReturnItem> riList = itemDAO.findByConCodes(conCodes);
				if (null != riList && riList.size() > 0) {
					for (Object o : pb.getResultList()) {
						ReturnInfo ret = (ReturnInfo) o;
						ret.setItemList(new ArrayList<ReturnItem>());
						for (ReturnItem ri : riList) {
							if (ret.getCode().intValue() == ri.getRc().intValue()) {
								ret.getItemList().add(ri);
							}
						}
					}
				}
			}
 		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MY_RETURN_LIST);
	 }
	 

	
	
	/**
	 * 显示标签
	 */
	@RequestMapping(Url.SHOW_TAG)
	public ModelAndView showTag(Integer code,HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (code != null) {
			ReturnInfo ret = retDAO.get(code);
			if (ret != null) {
				// 获得所有明细
				List<ReturnItem> itemList = itemDAO.findByConCode(code);
				// 获取所有详情
				List<ReturnDetail> detailList = detailDAO.findByRCode(code);
				
				// 循环将对应的详情放到对应的明细里面
				for (ReturnItem item : itemList) {
					//如果详情为空
					//if (null == item.getDetailList() || item.getDetailList().size() == 0)
						item.setDetailList(new ArrayList<ReturnDetail>());
					
					for (ReturnDetail detail : detailList) {
						// 判断明细的 编号 和 详情的编号相等的就放到对应的明细里面
						if (item.getCode().intValue() == detail.getRic().intValue()) {
							item.getDetailList().add(detail);
						}
					}
				}
				ret.setItemList(itemList);
				request.setAttribute(Parm.INFO, ret);
			}
		}
		return new ModelAndView(Url.MY_RETURN_SHOW_TAG);
	}
	

	

	@Autowired
	public void setRetDAO(ReturnInfoDAO retDAO) {
		this.retDAO = retDAO;
	}
	@Autowired
	public void setItemDAO(ReturnItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}

	@Autowired
	public void setDetailDAO(ReturnDetailDAO detailDAO) {
		this.detailDAO = detailDAO;
	}

}
